package cn.dankal.flyaudio.personal;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;

import com.bumptech.glide.request.target.BitmapImageViewTarget;

import androidx.appcompat.widget.AppCompatTextView;
import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;
import cn.dankal.basiclib.GlideApp;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.pojo.CardDetailEntity;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.basiclib.util.WebViewUtil;
import cn.dankal.basiclib.util.image.PicUtils;
import cn.dankal.basiclib.widget.DKWebView;
import cn.dankal.basiclib.widget.DkUserImageView;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.contract.CardDetailsContract;
import cn.dankal.flyaudio.friend.ChatShareActivity;
import cn.dankal.flyaudio.presenter.CardDetailPresenter;

/**
 * <pre>
 *     e-mail : junfeng.nie@duntech.com.cn
 *     time   : 13/7/2020
 *     desc   :
 *     version: cn.dankal.flyaudio.personal
 *     Copyright: Copyright（c）2018
 *     Company:
 * </pre>
 *
 * @author niejunfeng
 */
public class MyCardDetailActivity extends BaseActivity implements View.OnClickListener,
        CardDetailsContract.View {

    private CardDetailPresenter cardDetailPresenter;
    private AppCompatTextView tvCardName, tvUseRule, tvTime,
            tvName1, tvName, tvPrompt, tvConfirm, tvGiveAway;
    private DKWebView tvRule1;
    private DkUserImageView ivAvatar;
    private LinearLayout llTitle, llBottom;
    private CardDetailEntity entity;
    private String id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_card_detail;
    }

    @Override
    protected void initComponents() {
        cardDetailPresenter = new CardDetailPresenter();
        cardDetailPresenter.attachView(this);
        id = getIntent().getStringExtra("id");
        tvCardName = findViewById(R.id.tv_card_name);
        tvUseRule = findViewById(R.id.tv_useRule);
        tvTime = findViewById(R.id.tv_time);
        ivAvatar = findViewById(R.id.iv_avatar);
        tvRule1 = findViewById(R.id.tv_rule_1);
        llTitle = findViewById(R.id.ll_title);
        llBottom = findViewById(R.id.ll_bottom);
        tvConfirm = findViewById(R.id.tv_confirm);
        tvGiveAway = findViewById(R.id.tv_give_away);
        tvName1 = findViewById(R.id.tv_name_1);
        tvName = findViewById(R.id.tv_name);
        tvConfirm.setOnClickListener(this);
        tvGiveAway.setOnClickListener(this);
        cardDetailPresenter.getDetails(id);
        findViewById(R.id.iv_onback).setOnClickListener(this);


    }

    @Override
    public void onSuccess(CardDetailEntity cardDetailEntity) {
        this.entity = cardDetailEntity;
        tvCardName.setText(cardDetailEntity.getName());
        tvUseRule.setText("满" + cardDetailEntity.getAmount() + "元即可进行抵扣");
        String createTime = "";
        if(cardDetailEntity.getPublishedDatetime()!=null)
        if (cardDetailEntity.getPublishedDatetime().length() > 10) {
            createTime = cardDetailEntity.getPublishedDatetime().substring(0, 10);
        }
        String endTime = "";
        if(cardDetailEntity.getExpireDatetime()!=null)
        if (cardDetailEntity.getExpireDatetime().length() > 10) {
            endTime = cardDetailEntity.getExpireDatetime().substring(0, 10);
        }
        tvTime.setText("有效期:" + createTime + "~" + endTime);
        WebViewUtil.loadData(tvRule1, cardDetailEntity.getUseRule());
        String url = "";
        if ("USABLE".equals(cardDetailEntity.getState()) ||
                "USED".equals(cardDetailEntity.getState()) ||
                "EXPIRED".equals(cardDetailEntity.getState())) {
            if (cardDetailEntity.getFromUser() != null) {
                tvName.setText(cardDetailEntity.getFromUser().getUsername());
                url = cardDetailEntity.getFromUser().getAvatar();
            }
        }
        if ("TRANSFERRED".equals(cardDetailEntity.getState()) ||
                "TRANSFERRING".equals(cardDetailEntity.getState())) {
            if (cardDetailEntity.getToUser() != null) {
                tvName.setText(cardDetailEntity.getToUser().getUsername());
                url = cardDetailEntity.getToUser().getAvatar();
            }
        }
        GlideApp.with(this).asBitmap().centerCrop().error(R.mipmap.pic_ill_4).load(PicUtils.QINIU_DOMAIN + url).into(new BitmapImageViewTarget(ivAvatar) {
            @Override
            protected void setResource(Bitmap resource) {
                RoundedBitmapDrawable circularBitmapDrawable =
                        RoundedBitmapDrawableFactory.create(getResources(), resource);
                circularBitmapDrawable.setCircular(true);
                ivAvatar.setImageDrawable(circularBitmapDrawable);
            }
        });
        switch (cardDetailEntity.getState()) {
            //USABLE
            case "USABLE":
                llTitle.setBackgroundResource(R.mipmap.bg_card_details_1);
                llBottom.setVisibility(View.VISIBLE);
                tvName1.setText("来自：");
                break;
            case "USED":
                llBottom.setVisibility(View.GONE);
                tvName1.setText("来自：");
                break;
            case "EXPIRED":
                llBottom.setVisibility(View.GONE);
                tvName1.setText("来自：");
                break;
            case "TRANSFERRED":
                llBottom.setVisibility(View.GONE);
                llTitle.setBackgroundResource(R.mipmap.card_presented);
                tvName1.setText("赠送给：");
                break;
            case "TRANSFERRING":
                llBottom.setVisibility(View.GONE);
                llTitle.setBackgroundResource(R.mipmap.card_giving);
                tvName1.setText("赠送给：");
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_give_away:
                ////TODO 赠送
                Intent intent = new Intent();
                intent.setClass(this, ChatShareActivity.class);
                intent.putExtra("couponId", id);
                ActivityUtils.startActivity(intent);
                break;
            case R.id.tv_confirm:
                if(entity == null){
                    ToastUtils.showShort("无效优惠券!");
                    return;
                }
                Bundle bundle = new Bundle();
                bundle.putString("time", entity.getExpireDatetime());
                bundle.putString("code", id);
                bundle.putString("name", entity.getName());
                ActivityUtils.startActivity(bundle, CardQrCodeActivity.class);
                break;

            case R.id.iv_onback:
                onBackPressed();
                break;
        }
    }
}
